package com.ego.common.vo.product;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * @program: ego
 * @description: 购物车
 * @author: KamTang
 * @create: 2022-12-14 15:22
 **/
public class CartVo implements Serializable {

    private List<CartItem> items;

    /**
     * 商品总数量
     */
    private Integer numCount;

    /**
     * 商品类型数量
     */
    private Integer typeCount;

    /**
     * 减免价格(0)
     */
    private BigDecimal reduce = BigDecimal.ZERO;

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getNumCount() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (CartItem item : items) {
                count += item.getCount();
            }
        }
        return count;
    }

    public Integer getTypeCount() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (int i = 0; i < items.size(); i++) {
                count += 1;
            }
        }
        return count;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = BigDecimal.ZERO;
        if (items != null && items.size() > 0) {
            for (CartItem item : items) {
                if (item.getSelected()) {
                    amount = amount.add(item.totalPrice);
                }
            }
        }
        // 减去优惠价格
        return amount.subtract(getReduce());
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }

    /**
     * 购物项
     */
    @Setter
    @Getter
    public static class CartItem {
        private Long skuId;
        private Boolean selected = true;
        private String title;
        private String image;
        private List<String> skuAttrs;
        private BigDecimal price;
        private Integer count;
        private BigDecimal totalPrice;
        /**
         * 是否参与促销
         */
        private Integer promotion = 0;

        public BigDecimal getTotalPrice() {
            return this.price.multiply(new BigDecimal("" + this.count));
        }

        public void update(CartUpdateVo update) {
            this.setSelected(update.getSelected() == null ? selected : (update.getSelected() == 1));
            this.setCount(update.getCount() == null ? count : update.getCount());
        }
    }
}
